package com.lg.gexingqianming.ui;

import java.util.Iterator;

import org.json.JSONArray;
import org.json.JSONException;

import com.lg.data.Data;

import android.app.Activity;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	private ScrollView scrollView;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		scrollView = (ScrollView) findViewById(R.id.main_sv);

		// LinearLayout linearLayout = new LinearLayout(this);
		// LinearLayout.LayoutParams layoutParams = new
		// LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
		// ViewGroup.LayoutParams.FILL_PARENT);
		// layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
		// linearLayout.setOrientation(1);
		// scrollView.addView(linearLayout, layoutParams);
		//
		// ImageView imageView = new ImageView(this);
		// imageView.setBackgroundResource(R.drawable.header1);
		// LinearLayout.LayoutParams params = new
		// LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
		// ViewGroup.LayoutParams.WRAP_CONTENT);
		// linearLayout.addView(imageView, params);
		//
		// LinearLayout linearLayout2 = new LinearLayout(this);
		// LinearLayout.LayoutParams layoutParams1 = new
		// LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
		// ViewGroup.LayoutParams.WRAP_CONTENT);
		// linearLayout.addView(linearLayout2, layoutParams1);
		//
		// for (int i = 0; i < 3; i++) {
		// Button button = new Button(this);
		// LinearLayout.LayoutParams params1 = new
		// LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
		// ViewGroup.LayoutParams.WRAP_CONTENT);
		// params1.weight = 1;
		// button.setText("ddd");
		// button.setId(i);
		// button.setBackgroundResource(R.drawable.pink);
		// button.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// Toast.makeText(MainActivity.this, "id:"+v.getId(), 1000).show();
		// }
		// });
		// linearLayout2.addView(button, params1);
		// }

		// 总的线性布局
		LinearLayout ll = new LinearLayout(this);
		LinearLayout.LayoutParams llParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
		llParams.gravity = Gravity.CENTER_HORIZONTAL;
		ll.setOrientation(Data.VERTICAL);
		scrollView.addView(ll, llParams);

		JSONArray names = Data.menuJson.names();
		try {
			// 每个项目的线性布局
			for (int i = 0; i < names.length(); i++) {
				String lables = (String) Data.menuJson.get(names.getString(i));
				String[] split = lables.split("-");
				LinearLayout itemLl = new LinearLayout(this);
				LinearLayout.LayoutParams itemLlParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
				llParams.gravity = Gravity.CENTER_HORIZONTAL;
				itemLl.setOrientation(Data.VERTICAL);
				ll.addView(itemLl, itemLlParams);
				
				// 每个项目中的ImageView
				ImageView imageView=new ImageView(this);
				LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
				itemLl.addView(imageView, imageParams);
				
				int line=(split.length-split.length%3)/3+((split.length%3==0)?0:1);
				for (int j = 0; j < line; j++) {
					LinearLayout buttonLl = new LinearLayout(this);
					itemLl.addView(buttonLl, itemLlParams);
					int col=(j==line-1)?(split.length-j*3):3;
					for (int k = 0; k < col; k++) {
						Button button =new Button(this);
						LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
						buttonParams.weight=1;
						buttonLl.addView(button, buttonParams);
					}
				}

			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		// 每个项目中的线性布局

	}
}